package com.wc.shj.service.impl;

import cn.hutool.core.lang.UUID;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wc.shj.dto.UserDTO;
import com.wc.shj.mapper.UserMapper;
import com.wc.shj.pojo.User;
import com.wc.shj.service.IUserService;
import com.wc.shj.vo.ResultVO;
import com.wc.shj.vo.UserVO;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private RedisTemplate<String, Object> redisTemplate;


    @Override
    public ResultVO adminLogin(UserVO userVO) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("real_name", userVO.getRealName());
        queryWrapper.eq("password", userVO.getPassword());
        User userEntity = userMapper.selectOne(queryWrapper);
        //System.out.println("addsasa"+userEntity);
        if (userEntity == null) {
            return ResultVO.failure("此账号不存在！");
        } else if (!userEntity.getPassword().equals(userVO.getPassword())) {
            return ResultVO.failure("登录密码错误！");
        }
           /* else if(userEntity.getUserStatus() == 1){
                return ResultVO.failure("登录失败，此账号已被禁用！");
            }
            else if(userEntity.getUserType() != 0){
                return ResultVO.failure("登录失败，您不是后台管理员！");
            }*/
        else {
            String token = UUID.fastUUID().toString(true);
            UserDTO adminDTO = new UserDTO();
            adminDTO.setUserId(userEntity.getId());
            adminDTO.setRealName(userEntity.getRealName());
            adminDTO.setAvatarUrl(userEntity.getAvatarUrl());
            adminDTO.setToken(token);
            adminDTO.setPlot(userEntity.getPlot());
            adminDTO.setPlot(userEntity.getPlot());
            adminDTO.setProperty(userEntity.getProperty());
            Object adminObj = redisTemplate.opsForValue().get(token);
            if (adminObj == null) {
                redisTemplate.opsForValue().set(token, adminDTO, 30000000, TimeUnit.SECONDS);
            }
            return ResultVO.success("恭喜您，登录成功！", adminDTO);
        }
    }
}

